class stream {
  constructor(id, name, url, devId, channel, channelNo) {
    // 节点id
    this.id = id;
    // 流的名字
    this.name = name;
    // 请求地址
    this.url = url;
    this.pc = null;
    // 通道编号
    this.channelNo = channelNo || "";
    // 设备号
    this.devId = devId || "";
    // 通道号0,1,2,...
    this.channel = channel || "0";
  }
}
class replaystream {
  constructor(id, name, url, devId, channel, channelNo, starttime, endtime, time) {
    // 节点id
    this.id = id;
    // 流的名字
    this.name = name;
    // 请求地址
    this.url = url;
    this.pc = null;
    // 通道编号
    this.channelNo = channelNo || "";
    // 设备号
    this.devId = devId || "";
    // 通道号0,1,2,...
    this.channel = channel || "0";
    // 
    this.starttime = starttime;
    // 
    this.endtime = endtime;
    // 时间戳
    this.time = time
  }
}

class Webrtc {
  constructor(addr = "127.0.0.1") {
    this.streams = {};
    if (addr && addr.indexOf("http://") != 0) {
      addr = "http://" + addr;
    }
    if (addr && addr[addr.length - 1] != "/") {
      addr = addr + "/";
    }
    this.addr = addr;
  }
  // 添加流
  addStream(id, name, url, devId = "", channel = "", channelNo = "") {
    var s = new stream(id, name, url, devId, channel, channelNo);
    this.streams[name] = s;
  }
  // 添加回放流
  addrePlayStream(id, name, url, devId = "", channel = "", channelNo = "", starttime = "", endtime = "", time = "") {
    var s = new replaystream(id, name, url, devId, channel, channelNo, starttime, endtime, time);
    this.streams[name] = s;
  }
  // 播放流
  async play(name) {
    var s = this.streams[name];
    var vid = document.getElementById(s.id);
    var pc = new RTCPeerConnection();
    this.streams[name].pc = pc;
    pc.addTransceiver('video', {
      direction: 'recvonly'
    });
    await pc.setLocalDescription(await pc.createOffer());
    pc.ontrack = function (ev) {
      if (ev.track.kind == "video") {
        vid.srcObject = ev.streams[0];
      }
    }
    const result = await this._post(s.url, JSON.stringify(pc.localDescription.toJSON()));
    await pc.setRemoteDescription(result);
  }
  // 停止流
  stop(name) {
    // this.bye(name);
    this.streams[name].pc.close();
    this.streams[name].pc = null;
  }
  // 移除流
  remove(name) {
    delete this.streams[name];
  }
  // 拉取回放列表
  replaylist(name) {
    if (this.replaystream[name].devId) {
      this.streams[name].url = this.addr + "api/webrtc/play?streamPath=" + this.streams[name].devId + "/" + this.streams[name].starttime;
      return this._get(this.addr + "api/gb28181/invite?id=" + this.streams[name].devId + "&channel=" + this.streams[name].channel + "&startTime=" + this.streams[name].starttime + "&endTime=" + this.streams[name].endtime);
    }
  }
  // 邀请设备
  invite(name) {
    if (this.streams[name].devId) {
      this.streams[name].url = this.addr + "api/webrtc/play?streamPath=" + this.streams[name].devId + "/" + this.streams[name].channelNo;
      return this._get(this.addr + "api/gb28181/invite?id=" + this.streams[name].devId + "&channel=" + this.streams[name].channel);
    }
  }
  // 邀请回放设备
  invitereplay(name) {
    if (this.streams[name].devId) {
      this.streams[name].url = this.addr + "webrtc/play?streamPath=" + this.streams[name].channelNo + "/" + this.streams[name].time;
      return this._get(this.addr + "gb28181/invite?id=" + this.streams[name].devId + "&channel=" + this.streams[name].channel + "&startTime=" + this.streams[name].starttime + "&endTime=" + this.streams[name].endtime);
    }
  }
  // 断开流媒体服务中的设备连接，断开后如要重新播放，需要发送invite命令
  bye(name) {
    if (this.streams[name].devId) {
      return this._get(this.addr + "api/gb28181/bye?id=" + this.streams[name].devId + "&channel=" + this.streams[name].channel);
    }
  }
  // 异步get请求
  async _get(url) {
    return new Promise((resolve, reject) => {
      let xhr = new XMLHttpRequest();
      xhr.open("GET", url);
      xhr.onload = () => {
        resolve(xhr.status);
      }
      xhr.onerror = () => {
        reject(xhr.status);
      }
      xhr.send();
    }).catch(e => {
      console.log(e);
    })
  }
  // 异步post请求
  async _post(url, data) {
    return new Promise((resolve, reject) => {
      let xhr = new XMLHttpRequest();
      xhr.open("POST", url);
      xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      xhr.onload = () => {
        resolve(JSON.parse(xhr.responseText));
      }
      xhr.onerror = e => {
        reject(xhr.statusText);
      }
      xhr.send(data);
    }).catch(e => {
      console.log(e);
    });
  }
}
export {
  Webrtc
}
